package com.example.countscore;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import java.util.ArrayList;
import java.util.List;

//使用官方提供的list

public class MyListActivity extends ListActivity {

    Handler handler;
    public static final String TAG = "MyListActivity";

    //此时不需要加载
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //构造数据
//        List<String> list1 = new ArrayList<String>();
//        for(int i=1; i<100; i++){
//            list1.add("item"+i);
//        }
//
//        String[] list_data = {"one", "two", "three", "four"};
//        ListAdapter adapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1,list1);
//        setListAdapter(adapter);


        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg){
                Log.i(TAG, "handleMessage: 收到消息");

                if(msg.what==7){
                    //接收消息
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(
                            MyListActivity.this,    //当前activity的对象
                            android.R.layout.simple_list_item_1,
                            list2);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };

        //启动线程
        MyTask task = new MyTask();
        task.setHandler(handler);       //调用setHandler，使其不为空
        Thread thread = new Thread(task);
        thread.start(); //task.run()
    }
}